﻿package  
{
	import caurina.transitions.Equations;
	import caurina.transitions.Tweener;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;
	import tools.GraphicsTools;
	/**
	 * ...
	 * @author ben.
	 */
	public class Preloader extends MovieClip
	{
		
		public var loading_mc:MovieClip
		public var artH:MovieClip
		public var bg:MovieClip
		public var trame:MovieClip
		
		private var artH_loader:Loader
		private var cpt:Number = 0;
		private var bgTrame:Shape;
		
		public function Preloader() 
		{
			artH.alpha = 0;
			trame.alpha = 0;
			bg.visible = false;
			
			var cdv_menu:ContextMenu = new ContextMenu();
			cdv_menu.hideBuiltInItems();
			var copyright:ContextMenuItem = new ContextMenuItem("Copyright © art H 2010.",false, false);
			cdv_menu.customItems.push(copyright);
			
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			contextMenu = cdv_menu;
			artH_loader = new Loader();
			artH_loader.contextMenu = cdv_menu;
			
			Tweener.addTween(artH, { alpha:1, time:2, transition:Equations.easeOutQuad, onComplete:_onInit } );
			
			stage.addEventListener(Event.RESIZE, _onResize);
			_onResize();
		}
		
		private function _onInit():void
		{
			addEventListener(Event.ENTER_FRAME, _increment);
			artH_loader.load(new URLRequest("Main.swf"));
		}
		
		private function _increment(e:Event):void 
		{
			cpt ++;
			
			var r:Number = Math.round((artH_loader.contentLoaderInfo.bytesLoaded / artH_loader.contentLoaderInfo.bytesTotal) * 100);
			var goto:Number = Math.min(cpt, r);
			
			loading_mc.gotoAndStop(goto);
						
			if (goto >= 100)
			{
				removeEventListener(Event.ENTER_FRAME, _increment);
				Tweener.addTween(artH, { alpha:0, time:2, transition:Equations.easeOutQuad, onComplete:pleaseReady } );
				Tweener.addTween(trame, { alpha:1, time:2, transition:Equations.easeOutQuad } );
			}
		}
		
		
		
		private function _onResize(e:Event = null):void 
		{
			
			var hyp:Number = Math.sqrt(Math.pow(stage.stageWidth / 2,2) + Math.pow(stage.stageHeight / 2, 2));
			
			loading_mc.x = artH.x = bg.x = stage.stageWidth / 2;
			loading_mc.y = artH.y = bg.y = stage.stageHeight / 2;
			
			loading_mc.width = 5 * hyp;
			loading_mc.height = 5 * hyp;
			
			bg.width = stage.stageWidth;
			bg.height = stage.stageHeight;
			
			if (bgTrame != null) 
			{
				bgTrame.graphics.clear();
				trame.removeChild(bgTrame);
				bgTrame = null;
			}
			
			bgTrame = GraphicsTools.getVeilSh(stage.stageWidth, stage.stageHeight, 0x66666666);
			trame.addChild(bgTrame);
			
		}
		
		private function pleaseReady(e:Event = null):void 
		{
			bg.visible = true;
			artH.visible = false;
			loading_mc.visible = false;
			
			addChild(artH_loader);
			(artH_loader.content as Main).Init();
		}
		
	}

}